package cn.group7.padserver.service.impl;

import cn.group7.padserver.dao.PurchaseDao;
import cn.group7.padserver.entity.MyPurchase;
import cn.group7.padserver.pojo.Purchase;
import cn.group7.padserver.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 谷汉斌
 * @description
 * @createTime 2024/8/4 下午4:32
 */
@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseDao purchaseDao;

    @Override
    public boolean add(Purchase purchase) {
        return purchaseDao.insertSelective(purchase)==1;
    }

    @Override
    public Purchase selectNewPurchaseId(String ownName) {
        return purchaseDao.selectNewPurchaseId(ownName);
    }

    @Override
    public List<MyPurchase> selectByPurchaseType() {
        //获取用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        //创建Purchase实例，设置用户名，订单类型
        MyPurchase purchase = new MyPurchase();
        purchase.setOwnName(name);
        //查询
        return purchaseDao.selectByPurchase(purchase);
    }
}
